package cn.xgj.sfzy.controller;

import cn.xgj.sfzy.entity.Charge;
import cn.xgj.sfzy.service.ChargeService;
import cn.xgj.sfzy.util.Result;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author 张卫姣
 * @Date 2024年10月24日 16:19
 * @Version 1.0
 */
@RestController
@RequestMapping("/charge")
public class ChargeController {
    @Autowired
    private ChargeService chargeService;

    @PostMapping("/addCharge")
    public Result addCharge(@RequestBody Charge charge){
        Integer num = chargeService.add(charge);
        if (num!=0){
            return Result.success("添加成功！");
        }else{
            return Result.error("添加失败！");
        }

    }

    @DeleteMapping("/removeCharge/{chargeId}")
    public Result removeCharge(@PathVariable("chargeId") Integer chargeId){
        Integer num = chargeService.remove(chargeId);
        if (num!=0){
            return Result.success("删除成功！");
        }else{
            return Result.error("删除失败！");
        }

    }

    @PutMapping("/editCharge")
    public Result editCharge(@RequestBody Charge charge){
        Integer num = chargeService.edit(charge);
        if (num!=0){
            return Result.success("修改成功！");
        }else{
            return Result.error("修改失败！");
        }

    }

    @GetMapping("/findAll")
    public Result findAll(Charge charge){
        List<Charge> charges = chargeService.findAll(charge);
        return Result.success(charges);
    }

    @GetMapping("/findById/{chargeId}")
    public Result findById(@PathVariable("chargeId") Integer chargeId){
        Charge charge = chargeService.findById(chargeId);
        return Result.success(charge);
    }

    @GetMapping("/findByPage")
    public Result findByPage(
            @RequestParam(required = false,defaultValue = "1") Integer pageNum,
            @RequestParam(required = false,defaultValue = "5")Integer pageSize,Charge charge){

        PageInfo<Charge> pageInfo = chargeService.findByPage(pageNum, pageSize, charge);
        return Result.success(pageInfo);

    }
}
